package com.yycx.module.bus.provider.handler.process.task;

import com.yycx.bpm.provider.listener.FlowPageInterceptor;
import com.yycx.bpm.provider.model.TaskResponse;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.admin.client.entity.BaseUser;
import com.yycx.module.admin.provider.service.BaseUserService;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.service.*;
import com.yycx.module.system.client.entity.SysBank;
import com.yycx.module.system.provider.service.SysBankService;
import org.flowable.common.engine.api.query.Query;
import org.flowable.task.api.TaskInfoQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 市场保证金审批任务列表
 *
 * @author zyf
 */
@Component
public class CustomBondTaskTodoListHandler implements FlowPageInterceptor<TaskResponse> {

    @Autowired
    private BusPaymentService paymentService;
    @Autowired
    private BusSupplierService supplierService;

    @Autowired
    private BusDeptService deptService;

    @Autowired
    private BusCustomService customService;
    @Autowired
    private BaseUserService userService;
    @Autowired
    private BusOrderService orderService;


    @Autowired
    private SysBankService bankService;

    @Autowired
    private BusOrderDetailsService orderDetailsService;

    @Autowired
    private BusDrugsService drugsService;


    @Override
    public void prepare(Query query, EntityMap params) {
        TaskInfoQuery taskQuery = (TaskInfoQuery) query;
        String companyId=OpenHelper.getCompanyId().toString();
        String paymentNo = params.get("paymentNo");
        BusPayment payment = paymentService.getByProperty("paymentNo", paymentNo);
        if (FlymeUtils.isNotEmpty(payment)) {
            taskQuery.processInstanceBusinessKey(payment.getPaymentId().toString());
        } else {
            taskQuery.processInstanceBusinessKey(paymentNo);
        }
        taskQuery.taskTenantId(companyId);
        taskQuery.processDefinitionKey("process_custombond_"+companyId);
    }

    @Override
    public List<EntityMap> complete(Query query, List<TaskResponse> data, EntityMap params) {
        List<EntityMap> list = new ArrayList<>();
        if (FlymeUtils.isNotEmpty(data)) {
            for (TaskResponse taskResponse : data) {
                EntityMap map = new EntityMap();
                String paymentId = taskResponse.getBusinessKey();
                if (FlymeUtils.isNotEmpty(paymentId)) {
                    BusPayment payment = paymentService.getById(paymentId);
                    if (FlymeUtils.isNotEmpty(payment)) {
                        Long userId = payment.getUserId();
                        Long deptId = payment.getDeptId();
                        if (FlymeUtils.isNotEmpty(deptId)) {
                            BusDept dept = deptService.getById(deptId);
                            map.put("deptName", dept.getDeptName());
                            map.put("saleManId", dept.getSalemanId());
                        }
                        //单据类型
                        String amountType = payment.getAmountType();
                        BaseUser user = userService.getById(userId);
                        //回款
                        if (AmountTypeEnum.CustomRefund.getCode().equals(amountType)) {
                            getHuikuanMap(payment, map);
                        }
                        map.put("paymentNo", payment.getPaymentNo());
                        map.put("paymentId", payment.getPaymentId());
                        map.put("amountType", amountType);
                        map.put("amount", payment.getAmount());
                        map.put("nickName", user.getNickName());
                        map.put("createTime", payment.getCreateTime());
                        map.put("amountDate", DateUtils.formatDate(payment.getAmountDate(), "yyyy-MM-dd"));
                        map.put("paymentState", payment.getPaymentState());
                        map.put("assignee", taskResponse.getAssignee());
                        map.put("processInstanceId", taskResponse.getProcessInstanceId());
                        map.put("taskName", taskResponse.getName());
                        map.put("taskId", taskResponse.getId());
                        map.put("endTime", taskResponse.getEndTime());
                        map.put("orderId", payment.getOrderId());

                    }
                    list.add(map);
                }
            }
        }
        return list;
    }

    /**
     * 查询回款信息
     *
     * @param payment
     * @param map
     */
    private void getHuikuanMap(BusPayment payment, EntityMap map) {
        Long supplierId = payment.getSupplierId();
        String customId = payment.getCustomId();
        String orderId = payment.getOrderId();
        String orderDetailsId = payment.getOrderDetailsId();
        BusSupplier supplier = supplierService.getById(supplierId);
        BusCustom custom = customService.getById(customId);
        BusOrder order = orderService.getById(orderId);
        BusOrderDetails orderDetails = orderDetailsService.getById(orderDetailsId);
        //收款银行ID
        Long bankId = payment.getBankId();
        SysBank bank = bankService.getById(bankId);
        if (FlymeUtils.isNotEmpty(bank)) {
            map.put("bankName", bank.getBankName());
        }
        if (FlymeUtils.isNotEmpty(supplier)) {
            map.put("supplierName", supplier.getSupplierName());
        }
        if (FlymeUtils.isNotEmpty(custom)) {
            map.put("customName", custom.getCustomName());
        }
        if (FlymeUtils.isNotEmpty(order)) {
            map.put("orderNo", order.getOrderNo());
            map.put("sendDate", DateUtils.formatDate(order.getSendDate(), "yyyy-MM-dd"));
        }
        if (FlymeUtils.isNotEmpty(orderDetails)) {
            map.put("salePrice", orderDetails.getSalePrice());
            map.put("saleAmount", orderDetails.getSaleAmount());
            map.put("saleNum", orderDetails.getSaleNum());
            Long drugsId = orderDetails.getDrugsId();
            BusDrugs drugs = drugsService.getById(drugsId);
            if (FlymeUtils.isNotEmpty(drugs)) {
                map.put("drugsName", drugs.getDrugsName());
                map.put("drugsSpec", drugs.getDrugsSpec());
            }
        }
    }


}
